/**
 * @program: LeetCode
 * @description: LeetCode :
 * @author: WXY
 * @create: 2022-12-23 10:36
 * @Version 1.0
 **/

public class offer_six1_minWindow {
    public static String minWindow(String s, String t) {
        if (s.length() < t.length()) return "";
        int[] arr = new int[128];
        int strTLen = t.length();
        int strSLen = s.length();
        for (int i = 0; i < strTLen; i++) {
            arr[t.charAt(i)]++;
        }
        int[] cnt = arr.clone();
        //r 是右边界
        int l = 0;
        int r = 0;
        int left = 0;
        int right = 0;
        int curLen = Integer.MAX_VALUE;
        int minLen = Integer.MAX_VALUE;
        String ans = "";
        while (r < strSLen) {
            cnt[s.charAt(r++)]--;
            if (check(cnt)) {
                while (check(cnt)) {
                    cnt[s.charAt(l++)]++;
                }
                left = l - 1;
                right = r;
                curLen = right - left;
                minLen = Math.min(minLen, curLen);
                if (curLen <= minLen) {
                    ans = s.substring(left, right);
                }
            }
        }
        return ans;
    }

    private static boolean check(int[] cnt) {
        for (int i : cnt) {
            if (i > 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "ABBCCECODEBANNC";
        String t = "ABC";
        String m = minWindow(s, t);
        System.out.println(m);
    }
}
